#include <stdio.h>
#include <stdlib.h>
//联合体

union A
{
    int a1;
    short a2;
    char a3;
    int a4;
    int a5;
    char *p;
};

int main()
{
    union A a;
    a.a1 = 1;
    printf("sizeof(a) = %lu\n", sizeof(a));
    
    a.a3 = 10;
    a.a1 = 0;
    printf("a.a3 = %d\n", a.a3);

    printf("a.a1 = %p, a.a2 = %p, a.a3 = %p\n",&a.a1, &a.a2, &a.a3);
    //这三个地址一样，改变任何一个的值，另外两个也跟着改变
    a.a1 = 0x12345678;
    printf("十六进制a.a1 = %x\n", a.a1);
    a.a3 = 0;
    //一个int4个字节32位
    //一个十六进制数占4个二进制位
    //2^3 == 8  2^4 == 16
    //一个char2个字节8位
    printf("改变后十六进制a.a1 = %x\n", a.a1);

    //a.p = malloc(4);//假设这块堆内存的编号为0x12345678
    //a.a1 = 0;//p的值也成了0了,堆内存泄露
    //free(a.p);//无法free内存，堆内存泄漏


    return 0;
}

